package ru.st.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;
import org.openqa.selenium.support.ui.ExpectedConditions;


public class InternalPage extends AnyPage {

	public InternalPage(PageManager pages) {
		super(pages);
	}
	
  public InternalPage ensurePageLoaded() {
    super.ensurePageLoaded();
    wait.until(presenceOfElementLocated(By.cssSelector("nav")));
    return this;
  }
  
  @FindBy(css = "nav a[href $= '?go=profile']")
  private WebElement userProfileLink;

  @FindBy(css = "nav a[href $= '?go=users']")
  private WebElement userManagementLink;

  @FindBy(css = "nav a[href $= '?logout']")
  private WebElement logoutLink;
  
  @FindBy(css = "nav a[href $= '?go=add']")
  private WebElement addMovieLink;
  
  @FindBy(id="results")
  private WebElement ResAdd;
  
  @FindBy(id="q")
  private WebElement searchLink;
  
  
  @FindBys({ @FindBy (id = "results"),
	  @FindBy(tagName = "a")})
    private List<WebElement>  movieRef;
		
  
  @FindBy(className="content")	
  private WebElement NoMovie;
  
 
  
  
  public UserProfilePage clickUserProfilePage() {
    userProfileLink.click();
    return pages.userProfilePage;
  }

  public UserManagementPage clickUserManagementLink() {
    userManagementLink.click();
    return pages.userManagementPage;
  }
  
  public LoginPage clickLogoutLink() {
    logoutLink.click();
    wait.until(alertIsPresent()).accept();
    return pages.loginPage;
  }


   public AddMoviePage clickAddLink() {
	   addMovieLink.click();
	   return pages.addMoviePage;
	
   }

   public InternalPage sendSearchLink(String title) {
	   searchLink.clear();
	   searchLink.sendKeys(title+Keys.ENTER);
	   return this;
   }

   public FilmPage clickMovieLink() {
	   movieRef.get(0).findElement(By.tagName ("div")).click();
	   //movieDel.click();
	   return pages.filmPages;
   }

   
   public boolean isNoFilms (){
	   
	   return movieRef.isEmpty();
   }
   
  public int CountFilms (){
	   
	   return movieRef.size();
   }
  
  public void waitPresence(Object a){
	  	  wait.until(ExpectedConditions.visibilityOfElementLocated((By) a));
   }

  public void waitNotPresence(String title){
	  By.id("results");
	 wait.until(ExpectedConditions.invisibilityOfElementWithText(By.tagName("a"), title));
	 
  }
  
  public String getContent(){
  	 return NoMovie.getText();
  }

 
 public  WebElement getFilms(int i){
		  		
   return movieRef.get(i).findElement(By.className("title"));
  }


  
 
} 